package com.example.springboot.controller;

import com.example.springboot.dto.CarouselImageDTO;
import com.example.springboot.entity.Admin;
import com.example.springboot.entity.CarouselImage;
import com.example.springboot.mapper.CarouselImageMapper;
import com.example.springboot.service.CarouselImageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/carousel")
public class CarouselImageController {
    private static final Logger logger = LoggerFactory.getLogger(CarouselImageController.class);

    @Autowired
    private CarouselImageService carouselImageService;

    @Autowired
    private CarouselImageMapper carouselImageMapper;

    private void checkSuperAdmin(HttpServletRequest request) {
        String username = (String) request.getAttribute("username");
        if (username == null) {
            logger.warn("未从请求中获取到用户名");
            throw new IllegalArgumentException("未提供有效的用户名");
        }

        Admin admin = carouselImageMapper.findByUsername(username);
        if (admin == null || !"super".equals(admin.getRole())) {
            logger.warn("权限不足，用户: {}，角色: {}", username, admin != null ? admin.getRole() : "未知");
            throw new IllegalArgumentException("只有超级管理员可以操作轮播图");
        }
    }

    @GetMapping("/images")
    public ResponseEntity<List<CarouselImage>> getActiveCarouselImages() {
        List<CarouselImage> images = carouselImageService.getActiveCarouselImages();
        return ResponseEntity.ok(images);
    }

    @GetMapping("/all")
    public ResponseEntity<List<CarouselImage>> getAllCarouselImages(HttpServletRequest request) {
        checkSuperAdmin(request);
        List<CarouselImage> images = carouselImageService.getAllCarouselImages();
        return ResponseEntity.ok(images);
    }

    @PostMapping("/create")
    public ResponseEntity<CarouselImage> createCarouselImage(
            @RequestPart("dto") CarouselImageDTO dto,
            @RequestPart("image") MultipartFile imageFile,
            HttpServletRequest request) throws Exception {
        logger.info("收到创建轮播图请求，Content-Type: {}, dto: {}, image: {}",
                request.getHeader("Content-Type"),
                dto,
                imageFile != null ? imageFile.getOriginalFilename() : "null");
        checkSuperAdmin(request);
        CarouselImage carouselImage = carouselImageService.createCarouselImage(dto, imageFile);
        return ResponseEntity.ok(carouselImage);
    }

    @PutMapping("/{carouselId}")
    public ResponseEntity<CarouselImage> updateCarouselImage(
            @PathVariable Integer carouselId,
            @RequestPart("dto") CarouselImageDTO dto,
            @RequestPart(value = "image", required = false) MultipartFile imageFile,
            HttpServletRequest request) throws Exception {
        logger.info("收到更新轮播图请求，Content-Type: {}, carouselId: {}, dto: {}, image: {}",
                request.getHeader("Content-Type"),
                carouselId,
                dto,
                imageFile != null ? imageFile.getOriginalFilename() : "null");
        checkSuperAdmin(request);
        CarouselImage carouselImage = carouselImageService.updateCarouselImage(carouselId, dto, imageFile);
        return ResponseEntity.ok(carouselImage);
    }

    @DeleteMapping("/{carouselId}")
    public ResponseEntity<Void> deleteCarouselImage(
            @PathVariable Integer carouselId,
            HttpServletRequest request) {
        checkSuperAdmin(request);
        carouselImageService.deleteCarouselImage(carouselId);
        return ResponseEntity.noContent().build();
    }
}